"""
保单相关的 Pydantic 模型
用于 API 请求和响应的数据验证
"""
from typing import Optional
from pydantic import BaseModel, Field
from datetime import datetime, date
from decimal import Decimal


# 基础保单信息
class PolicyBase(BaseModel):
    """保单基础信息"""
    policy_number: str = Field(..., min_length=1, max_length=128, description="保单号")
    applicant_name: str = Field(..., min_length=1, max_length=128, description="投保人姓名")
    insured_name: Optional[str] = Field(None, min_length=1, max_length=128, description="被保人姓名")
    insured_count: Optional[int] = Field(None, ge=1, description="被保人数")
    insured_date: Optional[date] = Field(None, description="投保日期")
    start_date: Optional[date] = Field(None, description="保单生效日期")
    end_date: Optional[date] = Field(None, description="保单失效日期")
    premium: Optional[Decimal] = Field(None, decimal_places=2, description="保费")
    occupation: Optional[str] = Field(None, max_length=128, description="职业")
    company_id: Optional[str] = Field(None, max_length=50, description="保险公司编号")
    product_id: Optional[str] = Field(None, max_length=50, description="产品编号")
    risk_type_id: Optional[str] = Field(None, max_length=50, description="险种编号")
    issuance_type_id: Optional[str] = Field(None, max_length=50, description="出单类型编号")
    file_path: Optional[str] = Field(None, max_length=255, description="保单文件路径")


# 保单创建
class PolicyCreate(PolicyBase):
    """创建保单时的数据模型"""
    salesman_job_number: str = Field(..., min_length=1, max_length=50, description="销售员工号")


# 保单更新
class PolicyUpdate(BaseModel):
    """更新保单时的数据模型"""
    applicant_name: Optional[str] = Field(None, max_length=128, description="投保人姓名")
    insured_name: Optional[str] = Field(None, max_length=128, description="被保人姓名")
    insured_count: Optional[int] = Field(None, ge=1, description="被保人数")
    insured_date: Optional[date] = Field(None, description="投保日期")
    start_date: Optional[date] = Field(None, description="保单生效日期")
    end_date: Optional[date] = Field(None, description="保单失效日期")
    premium: Optional[Decimal] = Field(None, decimal_places=2, description="保费")
    occupation: Optional[str] = Field(None, max_length=128, description="职业")
    company_id: Optional[str] = Field(None, max_length=50, description="保险公司编号")
    product_id: Optional[str] = Field(None, max_length=50, description="产品编号")
    risk_type_id: Optional[str] = Field(None, max_length=50, description="险种编号")
    issuance_type_id: Optional[str] = Field(None, max_length=50, description="出单类型编号")
    file_path: Optional[str] = Field(None, max_length=255, description="保单文件路径")
    salesman_job_number: Optional[str] = Field(None, max_length=50, description="销售员工号")


# 保单响应
class PolicyResponse(PolicyBase):
    """保单响应数据模型"""
    salesman_job_number: str = Field(..., description="销售员工号")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")

    class Config:
        from_attributes = True


# 保单详情响应（包含销售员信息）
class PolicyDetailResponse(PolicyResponse):
    """保单详情响应数据模型（包含关联信息）"""
    salesman_name: Optional[str] = Field(None, description="销售员姓名")
    salesman_role_name: Optional[str] = Field(None, description="销售员角色")